<template>
    <div>
        <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            @closed="close"
            @open="open"
            :append-to-body="true"
            width="600px">
            <el-form :model="formdata" label-width="120px" ref="addform" :rules="rules">
                <el-form-item label="评估时间：" prop="bAssessmenTime">
                    <el-date-picker
                        v-model="formdata.bAssessmenTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="评估内容：" prop="bAssessmenContent">
                    <el-input
                        class="line-height"
                        type="textarea"
                        placeholder="请输入评估内容"
                        v-model="formdata.bAssessmenContent"
                        :autosize="{ minRows: 4}"
                        maxlength="1000"
                        show-word-limit
                        >
                    </el-input>
                </el-form-item>
                <el-form-item label="评语：" prop="bAssessmenMem">
                    <el-input
                        class="line-height"
                        type="textarea"
                        placeholder="请输入评语"
                        v-model="formdata.bAssessmenMem"
                        :autosize="{ minRows: 4}"
                        maxlength="1000"
                        show-word-limit
                        >
                    </el-input>
                </el-form-item>
                <el-form-item label="评估人：" prop="bAssessmenTeach">
                    <el-select v-model="formdata.bAssessmenTeach" placeholder="请选择">
                        <el-option
                            v-for="item in teacherlist"
                            :key="item.TID"
                            :label="item.T_Name"
                            :value="item.TID">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import {editAssessmen,addAssessmen} from '@/api/index.js'
import dateToggle from '@/common/js/age.js';
// 添加康复记录
export default {
    data() {
        return {
            dialogVisible:false,
            formdata:{
                bAssessmenTime:'',
                bAssessmenContent:"",
                bAssessmenTeach:'',
                bAssessmenMem:''
            },
            rules: {
                bAssessmenTime: [
                    { required: true, message: '时间不能为空', trigger: 'change' }
                ],
                bAssessmenContent: [
                    { required: true, message: '评估内容不能为空', trigger: 'blur' }
                ],
                bAssessmenTeach: [
                    { required: true, message: '评估人不能为空', trigger: 'blur' }
                ],
                bAssessmenMem: [
                    { required: true, message: '评语不能为空', trigger: 'blur' }
                ]
            },
        }
    },
    props:{
        bSID:{
            type:String,
            default:''
        },
        teacherlist:{
            type:Array,
            default:function() {
                return []
            }
        },
        title:{
            type:String,
            default:function(){
                return '新增评估记录'
            }
        },
        eidtinfo:{
            type:null
        }
    },
    created(){
    },
    methods:{

        open() {
            this.formdata.bAssessmenTime  = dateToggle.dateToggle('-','-','',new Date())
            this.$nextTick(() => {
                if(this.eidtinfo) {
                    this.formdata.bAssessmenTime = this.eidtinfo.AssessmenTime;
                    this.formdata.bAssessmenContent = this.eidtinfo.AssessmenContent;
                    this.formdata.bAssessmenTeach = this.eidtinfo.AssessmenTeach;
                    this.formdata.bAssessmenMem = this.eidtinfo.AssessmenMem;
                }
            })
        }, 
        save(){
            this.$refs.addform.validate((valid) => {
                if (valid) {
                    if(this.eidtinfo) {
                        editAssessmen({...this.formdata,bSRID:this.eidtinfo.SRID,bSID:this.bSID}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','修改成功！');
                                    this.dialogVisible = false;
                                    this.$emit('addsuccess',true);
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    } else {
                        let params = {
                            ...this.formdata
                        }
                        if(this.bSID) {
                            params.bSID = this.bSID
                        }
                        addAssessmen({...params}).then((res) => {
                            if(res) {
                                if(res.code == 200) {
                                    this.$util.tipsMsg('success','添加成功！');
                                    this.dialogVisible = false;
                                    this.$emit('addsuccess',true);
                                } else {
                                    this.$util.tipsMsg('error',res.message);
                                }
                            }
                        }).catch((err) => {

                        })
                    }
                } else {
                    return false;
                }
            })
        },
        close(){
            this.$refs.addform.resetFields();
        }
    }
}
</script>
<style lang="less" scoped>

</style>
